<?php
/**
 * User: aka
 * Time: 21:09
 */

namespace app\update\controller;

use app\tenno\model\UsersFile;
use app\validate\users\EditPassword;
use app\validate\users\ForgetPassword;
use app\validate\users\UpdateGameId;
use app\validate\users\UpdateUserFile;
use think\facade\Session;
use think\Request;

class Users
{
    private $userId;
    private $sessionEmailCode;
    private $sessionForgetPasswordEmailAddress;

    public function __construct()
    {
        $this->userId = Session::get('user_info.id');
        $this->sessionEmailCode = Session::get('forget_password_email_code');
        $this->sessionForgetPasswordEmailAddress = Session::get('forget_password_email_address');
    }

    public function __destruct()
    {
        unset(
            $this->userId,
            $this->sessionEmailCode,
            $this->sessionForgetPasswordEmailAddress
        );
    }

    private function changePassword(string $email, string $newPassword)
    {
        $condition = array();
        $condition['email'] = $email;
        $updateData = array();
        $updateData['password'] = get_encode_password($newPassword);
        $updateData['update_time'] = time();
        \app\tenno\model\Users::where($condition)->update($updateData);
        Session::delete('user_info');
        return json(['state' => 200]);
    }

    public function editPassword(Request $request)
    {
        $validate = new EditPassword();
        if (!$validate->check($request->param())) {
            return json($validate->getError());
        }else{
            $email = Session::get('user_info.email');
            $condition = array();
            $condition['password'] = get_encode_password($request->param('old_password'));
            $condition['email'] = $email;
            $userInfo = \app\tenno\model\Users::where($condition)->find();
            return empty($userInfo) ?
                json(['state' => 500])
                :
                $this->changePassword($email, $request->param('new_password'));
        }
    }

    public function forgetPassword(Request $request)
    {
        $validate = new ForgetPassword();
        if (!$validate->check($request->param())) {
            return json($validate->getError());
        }else{
            $email = $request->param('email');
            $emailCode = $request->param('email_code');
            $password = get_encode_password($request->param('password'));
            $condition = array();
            $condition['email'] = $request->param('email');
            $userInfo = \app\tenno\model\Users::where($condition)->find();
            return empty($userInfo) ?
                json(['state' => 503])
                :
                $this->forgetPasswordAction($email, $emailCode, $password);
        }
    }

    private function forgetPasswordAction(string $email, int $emailCode, string $password)
    {
        if ($emailCode != $this->sessionEmailCode){
            return json(['state' => 504]);
        }elseif ($email != $this->sessionForgetPasswordEmailAddress){
            return json(['state' => 505]);
        }else{
            $condition = array();
            $condition['email'] = $email;
            $updateData = array();
            $updateData['password'] = $password;
            \app\tenno\model\Users::where($condition)->update($updateData);
            return json(['state' => 200]);
        }
    }

    public function updateGameId(Request $request)
    {
        $validate = new UpdateGameId();
        if (!$validate->check($request->param())) {
            return json($validate->getError());
        }else{
            $bool = check_same_game_id($request->param('game_id'));
            if ($bool == true)
            {
                return json(['state' => 602]);
            }
            \app\tenno\model\Users::update(
                [
                    'id' => $this->userId,
                    'game_id' => $request->param('game_id')
                ]
            );
            Session::set('user_info.game_id', $request->param('game_id'));
            return json(['state' => 200]);
        }
    }

    private function updateUserFileAction(string $tag1, string $tag2, string $tag3, string $message)
    {
        $condition = array();
        $condition['uid'] = $this->userId;
        $updateData = array();
        $updateData['tag_1'] = $tag1;
        $updateData['tag_2'] = $tag2;
        $updateData['tag_3'] = $tag3;
        $updateData['message'] = $message;
        $updateData['update_time'] = time();
        UsersFile::where($condition)->update($updateData);
        return json(['state' => 200]);
    }

    private function createUserFileAction(string $tag1, string $tag2, string $tag3, string $message)
    {
        UsersFile::create([
            'uid' => $this->userId,
            'tag_1' => $tag1,
            'tag_2' => $tag2,
            'tag_3' => $tag3,
            'message' => $message
        ]);
        return json(['state' => 200]);
    }

    public function updateUserFile(Request $request)
    {
        $validate = new UpdateUserFile();
        if (!$validate->check($request->param())) {
            return json($validate->getError());
        }else{
            $userFile = UsersFile::file($this->userId)->find();
            return empty($userFile) ?
                $this->createUserFileAction(
                    $request->param('tag_1'),
                    $request->param('tag_2'),
                    $request->param('tag_3'),
                    $request->param('message')
                )
                :
                $this->updateUserFileAction(
                    $request->param('tag_1'),
                    $request->param('tag_2'),
                    $request->param('tag_3'),
                    $request->param('message')
                );
        }
    }

    private function changeStateOFFLine()
    {
        \app\tenno\model\Users::update(
            [
                'id' => $this->userId,
                'game_state' => 0
            ]
        );
        Session::set('user_info.game_state', 0);
        return json(['state' => 201]);
    }

    private function changeStateONLine()
    {
        \app\tenno\model\Users::update(
            [
                'id' => $this->userId,
                'game_state' => 1
            ]
        );
        Session::set('user_info.game_state', 1);
        return json(['state' => 202]);
    }

    public function updateUserGameState()
    {
        $userInfo = \app\tenno\model\Users::where(['id' => $this->userId])->find();
        $userGameState = $userInfo['game_state'];
        return $userGameState > 0 ?
            $this->changeStateOFFLine()
            :
            $this->changeStateONLine();
    }

    public function signOut()
    {
        Session::delete('user_info');
    }
}